home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlcpio.zip / ERROR.C < prev    next >
C/C++ Source or Header  |  1990-06-05  |  2KB  |  91 lines

  1. /* error.c -- error handler for noninteractive utilities
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie */
  19.  
  20. #include <stdio.h>
  21. #ifndef VPRINTF_MISSING
  22. #ifdef __STDC__
  23. #include <stdarg.h>
  24. #else
  25. #include <varargs.h>
  26. #endif
  27. #else
  28. #define va_alist args
  29. #define va_dcl int args;
  30. #endif
  31.  
  32. #ifdef STDC_HEADERS
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #else
  36. void exit ();
  37.  
  38. static char *
  39. strerror (errnum)
  40.      int errnum;
  41. {
  42.   extern char *sys_errlist[];
  43.   extern int sys_nerr;
  44.  
  45.   if (errnum > 0 && errnum < sys_nerr)
  46.     return sys_errlist[errnum];
  47.   return "Unknown system error";
  48. }
  49. #endif
  50.  
  51. /* Print the program name and error message MESSAGE, which is a printf-style
  52.    format string with optional args.
  53.    If ERRNUM is nonzero, print its corresponding system error message.
  54.    Exit with status STATUS if it is nonzero. */
  55. /* VARARGS */
  56. void
  57. #if !defined (VPRINTF_MISSING) && defined (__STDC__)
  58. error (int status, int errnum, char *message, ...)
  59. #else
  60. error (status, errnum, message, va_alist)
  61.      int status;
  62.      int errnum;
  63.      char *message;
  64.      va_dcl
  65. #endif
  66. {
  67.   extern char *program_name;
  68. #ifndef VPRINTF_MISSING
  69.   va_list args;
  70. #endif
  71.  
  72.   fprintf (stderr, "%s: ", program_name);
  73. #ifndef VPRINTF_MISSING
  74. #ifdef __STDC__
  75.   va_start (args, message);
  76. #else
  77.   va_start (args);
  78. #endif
  79.   vfprintf (stderr, message, args);
  80.   va_end (args);
  81. #else
  82.   _doprnt (message, &args, stderr);
  83. #endif
  84.   if (errnum)
  85.     fprintf (stderr, ": %s", strerror (errnum));
  86.   putc ('\n', stderr);
  87.   fflush (stderr);
  88.   if (status)
  89.     exit (status);
  90. }
  91.